/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.nanosl.norg.ent;

import java.io.Serializable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author Thilina
 */
@Entity
@Table(name = "person_has_address")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "PersonHasAddress.findAll", query = "SELECT p FROM PersonHasAddress p"),
    @NamedQuery(name = "PersonHasAddress.findByPersonNIC", query = "SELECT p FROM PersonHasAddress p WHERE p.personHasAddressPK.personNIC = :personNIC"),
    @NamedQuery(name = "PersonHasAddress.findByAddressAddressNumber", query = "SELECT p FROM PersonHasAddress p WHERE p.personHasAddressPK.addressAddressNumber = :addressAddressNumber"),
    @NamedQuery(name = "PersonHasAddress.findByAddressStreet", query = "SELECT p FROM PersonHasAddress p WHERE p.personHasAddressPK.addressStreet = :addressStreet"),
    @NamedQuery(name = "PersonHasAddress.findByAddressCityCity", query = "SELECT p FROM PersonHasAddress p WHERE p.personHasAddressPK.addressCityCity = :addressCityCity")})
public class PersonHasAddress implements Serializable {
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected PersonHasAddressPK personHasAddressPK;
    @JoinColumn(name = "address_type_code", referencedColumnName = "code")
    @ManyToOne(optional = false)
    private AddressType addressType;
    @JoinColumns({
        @JoinColumn(name = "address_address_number", referencedColumnName = "address_number", insertable = false, updatable = false),
        @JoinColumn(name = "address_street", referencedColumnName = "street", insertable = false, updatable = false),
        @JoinColumn(name = "address_city_city", referencedColumnName = "city_city", insertable = false, updatable = false)})
    @ManyToOne(optional = false)
    private Address address;
    @JoinColumn(name = "person_NIC", referencedColumnName = "NIC", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private Person person;

    public PersonHasAddress() {
    }

    public PersonHasAddress(PersonHasAddressPK personHasAddressPK) {
        this.personHasAddressPK = personHasAddressPK;
    }

    public PersonHasAddress(String personNIC, String addressAddressNumber, String addressStreet, String addressCityCity) {
        this.personHasAddressPK = new PersonHasAddressPK(personNIC, addressAddressNumber, addressStreet, addressCityCity);
    }

    public PersonHasAddressPK getPersonHasAddressPK() {
        return personHasAddressPK;
    }

    public void setPersonHasAddressPK(PersonHasAddressPK personHasAddressPK) {
        this.personHasAddressPK = personHasAddressPK;
    }

    public AddressType getAddressType() {
        return addressType;
    }

    public void setAddressType(AddressType addressType) {
        this.addressType = addressType;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (personHasAddressPK != null ? personHasAddressPK.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof PersonHasAddress)) {
            return false;
        }
        PersonHasAddress other = (PersonHasAddress) object;
        if ((this.personHasAddressPK == null && other.personHasAddressPK != null) || (this.personHasAddressPK != null && !this.personHasAddressPK.equals(other.personHasAddressPK))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.nanosl.norg.ent.PersonHasAddress[ personHasAddressPK=" + personHasAddressPK + " ]";
    }
    
}
